****
* Leader of the Pack?
* Changes in ‘Wolf Warrior Diplomacy’ After 
* A Politburo Collective Study Session

* Authors:
* Samuel Brazys
* Alexander Dukalskis
* Stefan Müller

** Note: The .do file 02_run_regressions.do imports the tweet-level dataset
** that contains the LSS scores and runs the regression models reported in the paper 
** and appendix

* Install the diff package by uncommenting the following line
* ssc install diff

* Set object "data" to my data directory location
global data= "/Users/smueller/Google Drive/papers/China_Twitter/replication_materials"

global results="/Users/smueller/Google Drive/papers/China_Twitter/replication_materials"

* Change working directory to "$data"
cd "/Users/smueller/Google Drive/papers/China_Twitter/replication_materials"

* Set more off (for page scrolling)
set more off
set matsize 11000

* Load Data
import delimited "data_tweets_en.csv"

** Generate day variable
generate double day=date(date, "YMD")

** Generate centered day running variable (0= Day of Xi) 
gen day_c=day-22431

** Generate account type code
encode type, gen(type_code)

** Generate account type code
encode type_diplomat, gen(type_dip)

** Generate individual ID
encode screen_name, gen(individual_id)

** Generate (average) number of tweets by account 
egen max_day=max(day_c)
egen min_day=min(day_c)
gen totaldays=max_day-min_day
bys screen_name: gen avgtweets=_N/totaldays

gen OECD=.
replace OECD=1 if country=="United Kingdom" | country=="United States" | country=="Switzerland" | country=="Spain" | country=="Slovenia" | country=="Slovakia" | country=="Poland" | country=="Netherlands" | country=="Malta" | country=="Japan" | country=="Ireland" | country=="Hungary" | country=="Greece" | country=="Germany" | country=="France" | country=="Finland" | country=="Denmark" | country=="Czech Republic" | country=="Canada" | country=="Belgium" | country=="Austria" | country=="Australia" |  country=="Turkey"
replace OECD=0 if OECD!=1 & country!="NA"
gen institution=1 if type_dip==3 | type_dip==4 | type_dip==5

*** Standardize Wolfy Measure by Account
bys individual_id: egen mean_wolfy=mean(wolfy_score)
bys individual_id: egen sd_wolfy=sd(wolfy_score)
gen std_wolfy=(wolfy_score-mean_wolfy)/ sd_wolfy

*** Generate time codes around different treatment windows
local i=1
while `i'<=115 {
gen time_code_`i'=0 if day_c>=-`i'
replace time_code_`i'=1 if day_c>=0 & day_c<=`i'
local i=`i'+1
}


*** 15, 30, 45, 60, 75, 90 *** - generatepre
local i=1
while `i'<=115 {
bys screen_name: egen pre_only_`i'=max(time_code_`i')
gen pre_`i'=0
replace pre_`i'=1 if pre_only_`i'==0
local i=`i'+1
}



** Save base data
save "Wolfy_Base_WINDOWS.dta", replace
estimates clear

*** Table 1 Diff-Diff compared to All

*** compared to non-OECD diplomat and state-based institutions
gen treated=0
replace treated=1 if OECD==1

*** Get data for parallel trends plot
preserve
rename std_wolfy WDD
gen treat="Treated" if treated==1
replace treat="Not Treated" if treated==0
gen time="Pre" if day_c<0
replace time="Post" if day_c>=0
keep WDD treat time day_c
keep if abs(day_c)<29
export delimited using "pretrend_all", replace
restore 

*** Model at +/- 100 window
eststo All: diff std_wolfy, t(treated) p(time_code_100) cluster(individual_id)
outreg2 using "RESULTS_DID.doc", append ctitle(All)
estimates clear
eststo All: reg std_wolfy treated time_code_100 time_code_100##treated, cluster(individual_id)
outreg2 using "RESULTS_FULL.doc", append ctitle(All)
estimates clear


local i=1
while `i'<=115 {
eststo all_`i': diff std_wolfy, t(treated) p(time_code_`i') cluster(individual_id)
local i=`i'+1
}

esttab using "window_results.csv", b(5) se(5) scalars(difference) stat(p_diff) plain replace


*** compared to all non-OECD diplomats
drop treated
gen treated=.
replace treated=1 if OECD==1 & type_code==1
replace treated=0 if OECD==0 & type_code==1

*** Get data for parallel trends plot
preserve
rename std_wolfy WDD
gen treat="Treated" if treated==1
replace treat="Not Treated" if treated==0
gen time="Pre" if day_c<0
replace time="Post" if day_c>=0
keep WDD treat time day_c
keep if abs(day_c)<29
export delimited using "pretrend_dip", replace
restore 

*** Model at +/- 100 window
eststo DIP: diff std_wolfy, t(treated) p(time_code_100) cluster(individual_id)
outreg2 using "RESULTS_DID.doc", append ctitle(All)
estimates clear
eststo DIP: reg std_wolfy treated time_code_100 time_code_100##treated, cluster(individual_id)
outreg2 using "RESULTS_FULL.doc", append ctitle(All)
estimates clear


local i=1
while `i'<=115 {
eststo all_`i': diff std_wolfy, t(treated) p(time_code_`i') cluster(individual_id)
local i=`i'+1
}

esttab using "window_results_DIP.csv", b(5) se(5) scalars(difference) stat(p_diff) plain

*** compared to non-OECD ambassadors
drop treated
gen treated=.
replace treated=1 if OECD==1 & type_dip==2
replace treated=0 if OECD==0 & type_dip==2

*** Get data for parallel trends plot
preserve
rename std_wolfy WDD
gen treat="Treated" if treated==1
replace treat="Not Treated" if treated==0
gen time="Pre" if day_c<0
replace time="Post" if day_c>=0
keep WDD treat time day_c
keep if abs(day_c)<29
export delimited using "pretrend_amb", replace
restore 

*** Model at +/- 100 window
estimates clear
eststo AMB: diff std_wolfy, t(treated) p(time_code_100) cluster(individual_id)
outreg2 using "RESULTS_DID.doc", append ctitle(All)
estimates clear
eststo AMB: reg std_wolfy treated time_code_100 time_code_100##treated, cluster(individual_id)
outreg2 using "RESULTS_FULL.doc", append ctitle(All)
estimates clear


local i=1
while `i'<=115 {
eststo all_`i': diff std_wolfy, t(treated) p(time_code_`i') cluster(individual_id)
local i=`i'+1
}

esttab using "window_results_AMB.csv", b(5) se(5) scalars(difference) stat(p_diff) plain replace

*** Compared to non-OECD staff
drop treated
gen treated=.
replace treated=1 if OECD==1 & type_dip==6
replace treated=0 if OECD==0 & type_dip==6

*** Get data for parallel trends plot
preserve
rename std_wolfy WDD
gen treat="Treated" if treated==1
replace treat="Not Treated" if treated==0
gen time="Pre" if day_c<0
replace time="Post" if day_c>=0
keep WDD treat time day_c
keep if abs(day_c)<29
export delimited using "pretrend_stf", replace
restore 

*** Model at +/- 100 window
estimates clear
eststo Staff: diff std_wolfy, t(treated) p(time_code_100) cluster(individual_id)
outreg2 using "RESULTS_DID.doc", append ctitle(All)
estimates clear
eststo Staff: reg std_wolfy treated time_code_100 time_code_100##treated, cluster(individual_id)
outreg2 using "RESULTS_FULL.doc", append ctitle(All)
estimates clear


local i=1
while `i'<=115 {
eststo all_`i': diff std_wolfy, t(treated) p(time_code_`i') cluster(individual_id)
local i=`i'+1
}

esttab using "window_results_STF.csv", b(5) se(5) scalars(difference) stat(p_diff) plain replace

*** Compared to non-OECD institutions
drop treated
gen treated=.
replace treated=1 if OECD==1 & institution==1
replace treated=0 if OECD==0 & institution==1

*** Get data for parallel trends plot
preserve
rename std_wolfy WDD
gen treat="Treated" if treated==1
replace treat="Not Treated" if treated==0
gen time="Pre" if day_c<0
replace time="Post" if day_c>=0
keep WDD treat time day_c
keep if abs(day_c)<29
export delimited using "pretrend_int", replace
restore 

*** Model at +/- 100 window
estimates clear
eststo INST: diff std_wolfy, t(treated) p(time_code_100) cluster(individual_id)
outreg2 using "RESULTS_DID.doc", append ctitle(All)
estimates clear
eststo INST: reg std_wolfy treated time_code_100 time_code_100##treated, cluster(individual_id)
outreg2 using "RESULTS_FULL.doc", append ctitle(All)
estimates clear


local i=1
while `i'<=115 {
eststo all_`i': diff std_wolfy, t(treated) p(time_code_`i') cluster(individual_id)
local i=`i'+1
}

esttab using "window_results_INST.csv", b(5) se(5) scalars(difference) stat(p_diff) plain replace


**** APENDIX *****


**** format for Covariate anaylsis 
use "Wolfy_Base_WINDOWS.dta", replace
estimates clear
tabulate individual_id, generate(dum)
tabulate country, gen(cdum)

gen time_code=time_code_100

*************************

*** Table A5 Diff-Diff w country and account FEs
*** compared to non-OECD diplomat and state-based institutions
gen treated=0
replace treated=1 if OECD==1
eststo All: diff std_wolfy, t(treated) p(time_code) cluster(individual_id) cov(cdum* dum*)
outreg2 using "RESULTS_DID_fe.doc", append ctitle(All)

*** compared to all non-OECD diplomats
drop treated
gen treated=.
replace treated=1 if OECD==1 & type_code==1
replace treated=0 if OECD==0 & type_code==1
eststo Diplomats: diff std_wolfy, t(treated) p(time_code) cluster(individual_id) cov(cdum* dum*)
outreg2 using "RESULTS_DID_fe.doc", append ctitle(All Diplomats)

*** compared to non-OECD ambassadors
drop treated
gen treated=.
replace treated=1 if OECD==1 & type_dip==2
replace treated=0 if OECD==0 & type_dip==2
eststo Ambassadors: diff std_wolfy, t(treated) p(time_code) cluster(individual_id) cov(cdum* dum*)
outreg2 using "RESULTS_DID_fe.doc", append ctitle(Ambassadors)

*** compared to non-OECD staff
drop treated
gen treated=.
replace treated=1 if OECD==1 & type_dip==6
replace treated=0 if OECD==0 & type_dip==6
eststo Staff: diff std_wolfy, t(treated) p(time_code) cluster(individual_id) cov(cdum* dum*)
outreg2 using "RESULTS_DID_fe.doc", append ctitle(Staff)

*** compared to non-OECD institution
drop treated
gen treated=.
replace treated=1 if OECD==1 & institution==1
replace treated=0 if OECD==0 & institution==1
eststo Institution: diff std_wolfy, t(treated) p(time_code) cluster(individual_id) cov(cdum* dum*)
outreg2 using "RESULTS_DID_fe.doc", append ctitle(Institution)

esttab using "TableA5.csv", b(5) se(5) plain replace

*** Table A6 Diff-Diff w country and account FEs and Covariates
estimates clear

*** compared to non-OECD diplomat and state-based institutions
drop treated
gen treated=0
replace treated=1 if OECD==1
eststo All: diff std_wolfy, t(treated) p(time_code) cluster(individual_id) cov(nwords avgtweets followers_count retweet_count favorite_count cdum* dum*)
outreg2 using "RESULTS_DID_fec.doc", append ctitle(All)

*** compared to all non-OECD diplomats
drop treated
gen treated=.
replace treated=1 if OECD==1 & type_code==1
replace treated=0 if OECD==0 & type_code==1
eststo Diplomats: diff std_wolfy, t(treated) p(time_code) cluster(individual_id) cov(nwords avgtweets followers_count retweet_count favorite_count cdum* dum*)
outreg2 using "RESULTS_DID_fec.doc", append ctitle(All Diplomats)

*** compared to non-OECD ambassadors
drop treated
gen treated=.
replace treated=1 if OECD==1 & type_dip==2
replace treated=0 if OECD==0 & type_dip==2
eststo Ambassadors: diff std_wolfy, t(treated) p(time_code) cluster(individual_id) cov(nwords avgtweets followers_count retweet_count favorite_count cdum* dum*)
outreg2 using "RESULTS_DID_fec.doc", append ctitle(Ambassadors)

*** compared to non-OECD staff
drop treated
gen treated=.
replace treated=1 if OECD==1 & type_dip==6
replace treated=0 if OECD==0 & type_dip==6
eststo Staff: diff std_wolfy, t(treated) p(time_code) cluster(individual_id) cov(nwords avgtweets followers_count retweet_count favorite_count cdum* dum*)
outreg2 using "RESULTS_DID_fec.doc", append ctitle(Staff)

*** compared to non-OECD institution
drop treated
gen treated=.
replace treated=1 if OECD==1 & institution==1
replace treated=0 if OECD==0 & institution==1
eststo Institution: diff std_wolfy, t(treated) p(time_code) cluster(individual_id) cov(nwords avgtweets followers_count retweet_count favorite_count cdum* dum*)
outreg2 using "RESULTS_DID_fec.doc", append ctitle(Institution)

esttab using "TableA6.csv", b(5) se(5) plain replace

*** Table A7 - Diff-diff with non-standardized wolfy scores
estimates clear

*** compared to non-OECD diplomat and state-based institutions
drop treated
gen treated=0
replace treated=1 if OECD==1
eststo All: diff wolfy_score, t(treated) p(time_code) cluster(individual_id)
outreg2 using "RESULTS_DIDns.doc", append ctitle(All)

*** compared to all non-OECD diplomats
drop treated
gen treated=.
replace treated=1 if OECD==1 & type_code==1
replace treated=0 if OECD==0 & type_code==1
eststo Diplomats: diff wolfy_score, t(treated) p(time_code) cluster(individual_id)
outreg2 using "RESULTS_DIDns.doc", append ctitle(All Diplomats)

*** compared to non-OECD ambassadors
drop treated
gen treated=.
replace treated=1 if OECD==1 & type_dip==2
replace treated=0 if OECD==0 & type_dip==2
eststo Ambassadors: diff wolfy_score, t(treated) p(time_code) cluster(individual_id)
outreg2 using "RESULTS_DIDns.doc", append ctitle(Ambassadors)

*** compared to non-OECD staff
drop treated
gen treated=.
replace treated=1 if OECD==1 & type_dip==6
replace treated=0 if OECD==0 & type_dip==6
eststo Staff: diff wolfy_score, t(treated) p(time_code) cluster(individual_id)
outreg2 using "RESULTS_DIDns.doc", append ctitle(Staff)

*** compared to non-OECD institution
drop treated
gen treated=.
replace treated=1 if OECD==1 & institution==1
replace treated=0 if OECD==0 & institution==1
eststo Institution: diff wolfy_score, t(treated) p(time_code) cluster(individual_id)
outreg2 using "RESULTS_DIDns.doc", append ctitle(Institution)

esttab using "TableA7.csv", b(5) se(5) plain replace

